<template>
  <div class="app-container">
    <!--工具栏-->
    <div v-if="crud.props.searchToggle">
      <!-- 搜索 -->
      <label class="el-form-item-label">表单名称</label>
      <el-input
        v-model="query.name"
        clearable
        placeholder="表单名称"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />
      <label class="el-form-item-label">表单编码</label>
      <el-input
        v-model="query.code"
        clearable
        placeholder="表单编码"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />
      <label class="el-form-item-label">流程</label>
      <el-select
        v-model="query.categoryId"
        clearable
        filterable
        size="small"
        placeholder="流程"
        class="filter-item"
        style="width: 200px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in modelOptions"
          :key="item.name"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <rrOperation :crud="crud" />
    </div>
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="表单名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="流程" prop="modelId" style="width: 370px">
            <el-select
              v-model="modelId"
              style="width: 370px"
              placeholder="请选择"
              @change="changeModel"
            >
              <el-option
                v-for="item in modelOptions"
                :key="item.name"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="表单类型">
            <el-radio
              v-for="item in dict.form_type"
              :key="item.id"
              v-model="form.type"
              :label="item.value"
            >{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="表单路径">
            <el-input v-model="form.formPath" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="表单名称" />
        <el-table-column prop="code" label="表单编码" />
        <el-table-column prop="modelName" label="模型名称" />
        <el-table-column prop="type" label="表单类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">定制设计</span>
            <span v-else>自定义绘制</span>
          </template>
        </el-table-column>
        <el-table-column prop="formPath" label="定制表单路径" />
        <el-table-column
          v-permission="['admin', 'activitiForm:edit', 'activitiForm:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <el-tooltip content="设计表单" placement="top">
              <el-button
                v-permission="permission.edit"
                size="mini"
                type="primary"
                @click="designForm(scope.row)"
              ><i
                class="el-icon-magic-stick el-icon--right"
              /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudActivitiForm from '@/api/workflow/activitiForm'
import crudActReModel from '@/api/workflow/actReModel'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'

// 选择模型名称
let selectModelName = ''
const defaultForm = {
  id: null,
  name: null,
  code: null,
  modelId: null,
  modelName: null,
  type: null,
  content: null,
  formPath: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'ActivitiForm',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '表单管理',
      url: 'api/activitiForm',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudActivitiForm }
    })
  },
  dicts: ['form_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'activitiForm:add'],
        edit: ['admin', 'activitiForm:edit'],
        del: ['admin', 'activitiForm:del']
      },
      rules: {
        id: [{ required: true, message: '主键不能为空', trigger: 'blur' }]
      },
      // 模型数据
      modelOptions: [],
      modelId: '',
      modelName: ''
    }
  },
  // 页面挂载时
  mounted: function() {
    this.getModelData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      selectModelName = ''
      // 获取流程
      this.getModelData()
    },
    // 新增前将选项的值值为空
    [CRUD.HOOK.beforeToAdd]() {
      this.modelId = ''
      selectModelName = ''
    },
    // 初始化编辑时候流程
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.modelId = form.modelId
      selectModelName = ''
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      crud.form.modelId = this.modelId
      crud.form.modelName = selectModelName
      return true
    },
    /**
     * 设计表单
     */
    designForm(row) {
      const { href } = this.$router.resolve({
        path: '/formdesign',
        query: { id: row.id }
      })
      window.open(href, '_blank')
    },
    /**
     * 获取模型
     */
    getModelData() {
      crudActReModel
        .getAllModels()
        .then(res => {
          this.modelOptions = res
        })
        .catch(() => {})
    },
    /**
     * 更改选择流程
     */
    changeModel(value) {
      this.modelOptions.forEach(function(data, index) {
        if (value === data.id) {
          selectModelName = data.name
        }
      })
    }
  }
}
</script>

<style scoped></style>
